<?php

class Report extends Controller {

	function index()
	{
	}

	function money($format = null)
	{
		global $USER_LEVEL;
		$itogo_null = array(
			'minus' => 0,
			'plus' => 0,
			'summ' => 0
		);
		$scheta = $this->MSchet->getList(array('user_id' => Auth::$user_id));
		$fixes = array();
		$itogo = array();
		$itogo['R'] = $itogo_null;
		$itogo['$'] = $itogo_null;
		$itogo['E'] = $itogo_null;
		$time_begin_count_new_children = 0;
		$time_end_count_new_children = 0;
		foreach($scheta as $schet)
		{
			$fix = $this->MSchetFix->getLast($schet['id']);
			$fixes[$schet['id']] = $fix;
			if(!isset($itogo[$schet['valuta']]))
			{
				$itogo[$schet['valuta']] = $itogo_null;
			}
			$last_time = @$last_fix['date'] ? strtotime($last_fix['date']) : 0;
			$platezhi_filters = array('schet_id' => $schet['id']);
			if(@$fix['id'])
			{
				$platezhi_filters['time_add<'] = strtotime($fix['date']);
				$time_end_count_new_children = max($time_end_count_new_children, strtotime($fix['date']));
				$fix2 = $this->MSchetFix->getLast($schet['id'], $fix['id']);
				if(@$fix2['id'])
				{
					$platezhi_filters['time_add>'] = strtotime($fix2['date']);
					$time_begin_count_new_children = max($time_begin_count_new_children, strtotime($fix2['date']));
				}
				$itogo[$schet['valuta']]['summ'] += $fix['summ'];
			}
			$platezhi = $this->MPlatezh->getList($platezhi_filters);
			foreach($platezhi as $platezh)
			{
				$itogo[$schet['valuta']]['minus'] += $platezh['komiss'];
				isPlatezhBuy($platezh) ? 
					$itogo[$schet['valuta']]['plus'] += $platezh['summ'] :
					$itogo[$schet['valuta']]['minus'] += $platezh['summ'];
			}
		}
		foreach($itogo as $valuta => $arr)
		{
			$itogo[$valuta]['balance'] = $itogo[$valuta]['plus'] - $itogo[$valuta]['minus'];
		}

		$me = $this->MUser->get(Auth::$user_id);
		$arr_fio = explode(" ", $me['fio']);
		$me['small_fio'] = $arr_fio[0]." ".(@$arr_fio[1] ? substr($arr_fio[1], 0, 2)."." : '')." ".(@$arr_fio[2] ? substr($arr_fio[2], 0, 2)."." : '');
		$me['level_str'] = $USER_LEVEL[$me['level']];

		$count_children = $this->MUser->getCount(array('parent_id' => Auth::$user_id));

		$count_children_oper = $this->MUser->getCount(array('parent_id' => Auth::$user_id, 'has_oper' => true));


		$filters_count_new_users = array('parent_id' => Auth::$user_id, 'time_add>=' => $time_begin_count_new_children, 'date>=' => date("Y-m-d", $time_begin_count_new_children));
		$time_end_count_new_children = @$time_end_count_new_children ? $time_end_count_new_children : time();
		$filters_count_new_users['time_add<'] = $time_end_count_new_children;
		$count_new_users = $this->MUser->getCount($filters_count_new_users);

		$data = array('fixes' => $fixes, 'scheta' => $scheta, 'itogo' => $itogo, 'me' => $me, 'count_children' => $count_children, 'count_children_oper' => $count_children_oper, 'count_new_users' => $count_new_users, 'time_end' => $time_end_count_new_children);

		if($format == 'excel')
		{
			$this->load->view('report/money_excel', $data);
		}
		else
		{
			$this->load->view('head', array());
			$this->load->view('report/money', $data);
			$this->load->view('foot');
		}

	}
	
}

